<?php

/**
 * 
 */
class PostsController extends AppController 
{
	public $helpers = array('Html', 'Form');
	public function index() 
	{
		$this->set('posts', $this->Post->find('all'));
	}
	  
	public function view($id = null)
	{
		if (!$id)
		{
			throw new NotFoundException (__('Invalid post'));;
		}
		$post = $this->Post->findById($id);
		if (!$post)
		{
			throw new NotFoundException(__('Invalid post'));
		}
		$this->set('post',$post); # aqui la variable post pasa al view
	}
	public function add() 
	{
		if ($this -> request -> is('post')) 
		{  
			$this->Post->create();
			if ($this -> Post -> save($this -> request -> data)) 
			{
				$this -> Session->setFlash(__('Your post has been saved.'));
				$this -> redirect(array('action' => 'index'));
			} 
			else 
			{
				$this->Session->setFlash(__('Unable to add your post.'));
			}
		}
	}
	public function edit($id = null)
	{
		if (!$id)
		{
			throw new NotFoundException(__('Invalid Post'));
		}
		$post = $this -> Post -> findById($id);
		
		if (!$post)
		{
			throw new NotFoundException(__('Invalid post'));
		}
		if ($this -> request -> is('post') || $this -> request -> is('put'))
		{
			$this -> Post -> id = $id;	
			if ($this -> Post -> save($this -> request -> data))
			{
				$this -> Session -> setFlash(__('Your post has been updated.'));
				$this -> redirect(array('action' => 'index'));
			}
			else 
			{
				$this -> Session -> setFlash(__('Unable to update your post.'));
			}
			
		}
		if (!$this -> request -> data)
		{
			$this -> request -> data = $post;
		}
	}
	public function delete($id)
	{
		if($this -> request -> is('get'))
		{
			throw new MethodNotAllowedException();
		}
		if ($this -> Post -> delete($id))
		{
			$this->Session->setFlash(__('The post with id: %s has been deleted.', $id));	
			$this -> redirect(array('action' => 'index'));
		}
	}
}
